/*
 * @Author: likang xie 
 * @Date: 2018-06-12 11:31:21 
 * @Purpose: 程序公共脚本
 */

// 判断window对象
var global = window == window.parent ? window : window.parent;

// 全局变量
var timeout = 100000;

// btgo实例
var btgo = {
  cookie: {
    get: function get(n) {
      var m = document.cookie.match(new RegExp("(^| )" + n + "=([^;]*)(;|$)"));
      return !m ? "" : unescape(m[2]);
    },
    set: function set(name, value, domain, path, hour) {
      var expire = new Date();
      expire.setTime(expire.getTime() + (hour ? 3600000 * hour : 30 * 24 * 60 * 60 * 1000));
      document.cookie = name + "=" + value + "; " + "expires=" + expire.toGMTString() + "; path=" + (path ? path : "/") + "; " + (domain ? "domain=" + domain + ";" : "");
    },
    del: function del(name, domain, path) {
      document.cookie = name + "=; expires=Mon, 26 Jul 1997 05:00:00 GMT; path=" + (path ? path : "/") + "; " + (domain ? "domain=" + domain + ";" : "");
    },
    clear: function clear() {
      var rs = document.cookie.match(new RegExp("([^ ;][^;]*)(?=(=[^;]*)(;|$))", "gi"));
      for (var i in rs) {
        document.cookie = rs[i] + "=;expires=Mon, 26 Jul 1997 05:00:00 GMT; path=/; ";
      }
    }
  },
  request: function request($, options) {
    var _ref = options ? options : {},
      _ref$type = _ref.type,
      type = _ref$type === undefined ? 'GET' : _ref$type,
      url = _ref.url,
      _ref$data = _ref.data,
      data = _ref$data === undefined ? {} : _ref$data,
      _ref$success = _ref.success,
      success = _ref$success === undefined ? null : _ref$success,
      _ref$error = _ref.error,
      error = _ref$error === undefined ? null : _ref$error,
      _ref$async = _ref.async,
      async = _ref$async === undefined ? true : _ref$async;

    $.ajax({
      type: type,
      url: url,
      data: data,
      async: async,
      dataType: 'json',
      timeout: 100000,
      success: success,
      error: error
    });
  },
  msg: function msg(str) {
    global.layer.msg(str);
  },
  load: function load() {
    return global.layer.load(2);
  },
  close: function close(i) {
    i ? global.layer.close(i) : global.layer.closeAll();
  },
  alert: function alert(options) {
    var _ref2 = options ? options : {},
      _ref2$str = _ref2.str,
      str = _ref2$str === undefined ? '没有内容' : _ref2$str,
      _ref2$skin = _ref2.skin,
      skin = _ref2$skin === undefined ? '' : _ref2$skin,
      _ref2$yes = _ref2.yes,
      _yes = _ref2$yes === undefined ? null : _ref2$yes;

    global.layer.open({
      title: '提示',
      content: str,
      skin: skin,
      yes: function yes(index) {
        layer.close(index);
        _yes && _yes();
      }
    });
  },
  confirm: function confirm(options) {
    var _ref3 = options ? options : {},
      _ref3$str = _ref3.str,
      str = _ref3$str === undefined ? '没有内容' : _ref3$str,
      _ref3$success = _ref3.success,
      success = _ref3$success === undefined ? null : _ref3$success,
      _ref3$skin = _ref3.skin,
      skin = _ref3$skin === undefined ? '' : _ref3$skin;

    global.layer.confirm(str, {
      skin: skin
    }, function (index) {
      layer.close(index);
      success && success();
    });
  },
  getRsa: function getRsa($) {
    $.getScript("/static/js/rsa/jsbn.js", function (e) {
      $.getScript("/static/js/rsa/prng4.js", function (e) {
        $.getScript("/static/js/rsa/rng.js", function (e) {
          $.getScript("/static/js/rsa/rsa.js", function (e) {
            rsa = new RSAKey();
            var modulus = "CAC33AC3CECB1998ABB0E1B8FF0C3234B10EA5F4525295ACD94BEFC5926C477725113275297907CBBAB32DEE4EC98BDEE7B4F11F8AC05714297FE5F8D754101D39B2151CCA8EDBF98EE8620D34D429CA62CF6D78B441B69150951E68133BFE622B7C7BEB7CBED20A6DED001C860EC2E842DCBC09CB91DD3FFB566A26BB54A421";
            var exponent = "10001";
            rsa.setPublic(modulus, exponent);
          });
        });
      });
    });
  },
  encrypt: function encrypt(form) {
    if (typeof form == 'string') {
      return rsa.encrypt(form);
    } else {
      var str = '';
      for (var key in form) {
        if (form.hasOwnProperty(key)) {
          str += form[key] + '&';
        }
      }
      return rsa.encrypt(str.slice(0, str.length - 1));
    }
  },
  enter: function enter($, options) {
    var _ref4 = options ? options : {},
      _ref4$el = _ref4.el,
      el = _ref4$el === undefined ? '' : _ref4$el,
      _ref4$callback = _ref4.callback,
      callback = _ref4$callback === undefined ? null : _ref4$callback;

    $(el).on('keyup', function (event) {
      var e = event || window.event;
      if (e.keyCode == 13) {
        callback && callback();
      }
    });
  },
  sendCode: function sendCode(ele, time) {
    var startTime = time;
    var _ref5 = [time || 10, null],
      time = _ref5[0],
      timer = _ref5[1];

    ele.attr('disabled', 'disabled').html('再次发送(' + time + ')');
    timer = setInterval(function () {
      time -= 1;
      if (time == 0) {
        clearInterval(timer);
        ele.removeAttr('disabled').html('重新获取');
        time = startTime;
      } else {
        ele.html("\u518D\u6B21\u53D1\u9001(" + time + ")");
      }
    }, 1000);
  },
  getRequest: function getRequest() {
    var url = decodeURIComponent(window.location.search); //获取url中含"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  },
  reload: function reload() {
    var t = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1000;

    setTimeout(function () {
      window.location.reload();
    }, t);
  }
};

// 搜索框获取焦点去除val
layui.use('jquery', function () {
  var $ = layui.$;
  $(document).on('click', '.layui-form-selected input', function () {
    var val = $(this).val();
    if (val) {
      $(this).val('');
    } else {
      return false;
    }
  });
});

// 表单弹窗所需
var form = null;
layui.use('form', function () {
  form = layui.form;
})


function LayuiCreateSelect(selectId, url, value) {
  $.post(url, function(data){

      if (selectId.indexOf("#") != 0) {
          selectId = "#" + selectId;
      }

    $(selectId).empty();//清空该元素
    $(selectId).append("<option value=''>请选择</option>");

    for (var k in data) {
        $(selectId).append("<option value='"+data[k].channelid+"'>"+data[k].channel_name+"</option>");
    }

    layui.use('form', function(){
        var form = layui.form;
        //设置选中值
        if (value != undefined && value != null && value != '') {
          $(selectId).val(value);
        }
      form.render();
    });

  });
}

function layuiPhotos(images) {
  layer.photos({
    photos: {
      "title": "图片",
      "id": 'showImages',
      "data": images
    },
    anim:5
  });
}

function changeHomwAgentLevelIcon(src,agentLevelName) {
  document.getElementById('agent_level_icon').src = src;
  updateTxt = agentLevelName == 'S级代理' ? '满级' : '升级';
  html = agentLevelName + '(<a class="agent_level_color" href="?r=agentLevel/index" target="content">' + updateTxt +'</a>)';
  document.getElementById('agent_level_txt').innerHTML = html;
}

function changeAgentsCoins() {
  var ajax = new XMLHttpRequest();
  ajax.open('get',"?r=agentsCoin/index&mode=ajax&action=agentsCoinNum");
  ajax.send();
  ajax.onreadystatechange = function () {
    if (ajax.readyState==4 &&ajax.status==200) {
      responseText = JSON.parse(ajax.responseText);
      document.getElementById('agents-coins').innerText = responseText.data.num;
    }
  }
}

